package com.itheima.demo06;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CountDownLatch;

/**
 * @author Zhang_Han
 * @version 1.0
 * @date 2020/7/15 11:36
 */
public class MyRunnable03 implements Runnable {
    private String pathName;
    private CountDownLatch cdl;
    private ConcurrentMap<String, String> map;

    public MyRunnable03(String pathName, CountDownLatch cdl, ConcurrentMap<String, String> map) {
        this.pathName = pathName;
        this.cdl = cdl;
        this.map = map;
    }

    @Override
    public void run() {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        //创建字符输入流;
        try {
            bis = new BufferedInputStream(new FileInputStream(pathName));
            File file = new File(pathName);
            bos = new BufferedOutputStream(new FileOutputStream("E:\\copy\\" + file.getName()));
            //开始读写数据;
            byte[] arr = new byte[1024 * 8];
            int len;
            while ((len = bis.read(arr)) != -1) {
                bos.write(arr, 0, len);
                bos.flush();
            }
            //给map集合中添加元素;
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            map.put(pathName, sdf.format(date));
            //告诉主线程我执行完成了;
            cdl.countDown();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bis.close();
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

